package com.hktf.corelib.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class DESUtils {
    /**
     * 密钥
     */
    public static final String DEFAULT_KEY = "sjz.hbbill.com";

    /**
     * 解密
     * @param message 加密后的内容
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static String decrypt(String message, String key) throws Exception {

        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("gb2312"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("gb2312"));

        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte,"gb2312");
    }

    /**
     * 加密
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(String message, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

        return cipher.doFinal(message.getBytes("UTF-8"));
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }

        return digest;
    }

    /**
     * 加密，返回的是加密后的字符串
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String encrypt2(String message, String key) throws Exception {
        String jiami = java.net.URLEncoder.encode(message, "utf-8")
                .toLowerCase();
        String a = toHexString(encrypt(jiami, key)).toUpperCase();
        return a;
    }

    /**
     * 解密，返回的是解密后的字符串
     *
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String decrypt2(String message, String key) throws Exception {
        String result = java.net.URLDecoder.decode(decrypt(message, key),
                "utf-8");
        return result;
    }


    public static void main(String[] args) throws Exception {
        String key = "2CA7664E";
        String jiami = "00B13765-EB62-4592-8369-815794A314FD|70001|";

        String result = toHexString(encrypt(jiami, key)).toUpperCase();


        byte[] bufferResult = result.getBytes();
        byte[] buffer = new byte[bufferResult.length*2];
        for(int i=0;i<bufferResult.length;i++){
            buffer[i*2] =bufferResult[i];
            buffer[i*2+1]=0;
        }
        byte[] byteLength = new byte[4];
        byteLength[0] = (byte) (0xff & buffer.length);
        byteLength[1] = (byte) ((0xff00 & buffer.length) >> 8);
        byteLength[2] = (byte) ((0xff0000 & buffer.length) >> 16);
        byteLength[3] = (byte) ((0xff000000 & buffer.length) >> 24);


    }


    public static byte[] encodingStr(String str) throws Exception{
        String key = "2CA7664E";
        String result = toHexString(encrypt(str, key)).toUpperCase();
        byte[] bufferResult = result.getBytes();
        byte[] buffer = new byte[bufferResult.length*2];
        for(int i=0;i<bufferResult.length;i++){
            buffer[i*2] =bufferResult[i];
            buffer[i*2+1]=0;
        }
        return buffer;
    }

    public static byte[] encodingSt2(String str) throws Exception{
        String key = "2CA7664E";
        String result = toHexString(encrypt(str, key)).toUpperCase();
        return result.getBytes();
    }


    public static String decodingStr(String str) throws Exception{
        String key = "2CA7664E";
        return decrypt(str, key);
    }










    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }

        return hexString.toString();
    }
}